//
//  HNCommentMouldVC.m
//  优学荟管理系统
//
//  Created by 星道 on 2017/10/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNCommentMouldVC.h"

#import "ZYCommentContentController.h"



@interface HNCommentMouldVC ()
{
    IBOutlet UITableView *_TableView;
}
@property (nonatomic, strong) HNCommentMouldVM *VM;
@end

@implementation HNCommentMouldVC

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"评论模板";
    // 配置tableView
    [self.VM ConfigTableView:_TableView];
    /** 添加 */
    UIButton *Button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [Button addTarget:self action:@selector(Add) forControlEvents:(UIControlEventTouchUpInside)];
    [Button setBackgroundImage:[UIImage imageNamed:@"icon_add_default"] forState:(UIControlStateNormal)];
    [Button setBackgroundImage:[UIImage imageNamed:@"icon_add_default"] forState:(UIControlStateHighlighted)];
    Button.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button];
}


#pragma mark - Action





/** 添加 */
- (void)Add {
    /** 添加模板，回传模板标题 */
    [self.VM AddComplete:^(NSString *title) {
        [self addMoBanTitle:title];
    } name:@"添加模板"];
}





/** 删除 */
- (void)Cell:(HNCommentMouldCell *)Cell DeleteWithModel:(HNCommentMouldModel *)Model {

    [self.VM Delete:Model.ID Complete:^{
        // 删除成功之后回调
        [self.VM ReloadTableView:Model.ID];
    }];
}

/** 修改 */
- (void)Cell:(HNCommentMouldCell *)Cell UploadWithModel:(HNCommentMouldModel *)Model {

    [self.VM AddComplete:^(NSString *title) {
        [self upDateMoBanName:title titleId:[NSString stringWithFormat:@"%@",Model.ID]];
    } name:@"修改模板"];
}

/** 查看 */
- (void)Cell:(HNCommentMouldCell *)Cell LookUpWithModel:(HNCommentMouldModel *)Model {


    ZYCommentContentController *view = [ZYCommentContentController new];
    view.ID =  [NSString stringWithFormat:@"%@",Model.ID];
    view.oid = [NSString stringWithFormat:@"%@",Model.oid];
   [self.navigationController pushViewController:view animated:YES];

    
    
}




#pragma mark - Get
- (HNCommentMouldVM *)VM {
    if (!_VM) {
        _VM = [[HNCommentMouldVM alloc] init];
        _VM.VC = self;
    }
    return _VM;
}






#pragma mark ---- 添加模板的方法的实现
- (void)addMoBanTitle:(NSString *)title{

    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=saveTemplate&templateName=%@",title];
    NSString *changeUilStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            
           
            [self.VM RefreshTableView];
            
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        
    }];

    
    
}


#pragma mark ---- 修改模板的方法的实现
- (void)upDateMoBanName:(NSString *)name titleId:(NSString *)ID{
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=updateTemplateByOid&id=%@&templateName=%@",ID,name];
    NSString *changeUilStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            
            
            [self.VM RefreshTableView];
            
            
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        
        
        
    }];
    
    

    
    
}

#pragma mark ---- 添加模板的方法的实现





@end
